#-*-coding:utf-8-*-
from celery import task
from ITAdmin.settings import SALT_SERVER
from utils.salt import get_data
from models import SaltCommand, SaltLog


@task
def salt_command(token, tgt, method, cmd_id, user_id):
    headers = {"Content-type": "application/json", 'X-Auth-Token': token}
    params = {'client': 'local', 'fun': method, 'expr_form': 'list', 'tgt': tgt}
    result = get_data(SALT_SERVER, params, headers=headers)
    s = SaltLog(cmd_id=cmd_id, minions=tgt, user_id=user_id, result=result)
    s.save()


